﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace carportal
{
    public partial class offer_cars : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
             
            if (Session["UserID"]==null)
            {
                Response.Redirect("/signinfirst.aspx");
            }
            Page.Form.Attributes.Add("enctype", "multipart/form-data");
        }

        protected void SqlDataSource2_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {

        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Panel1.Visible = false;
            Panel2.Visible = true;
        }

       

        protected void Button4_Click(object sender, EventArgs e)
        {
            Panel1.Visible = false;
            Panel3.Visible = true;
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            Panel3.Visible = false;
            Panel1.Visible = true;
        } 

        protected void DropDownList7_Load(object sender, EventArgs e)
        {
          
        }

        protected void DropDownList1_Load(object sender, EventArgs e)
        {
           
        }

        protected void DropDownList1_Init(object sender, EventArgs e)
        {
            DropDownList dr = sender as DropDownList;
            dr.Items.Add(new ListItem("Please select", "-1"));
        }

        protected void DropDownList7_Init(object sender, EventArgs e)
        {
            DropDownList dr = sender as DropDownList;
            ListItem li = new ListItem("Select Country", "-1");
            dr.Items.Add(li);
        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            
            SqlDataSourceNewCar.Insert();

            //string sID = SqlDataSourceNewCar.InsertParameters["NewCarID"].DefaultValue.ToString();
            if (FileUpload1.HasFile)
                SaveUploadedfile(FileUpload1, "001.");
            if (FileUpload2.HasFile)
                SaveUploadedfile(FileUpload2, "002.");
            if (FileUpload3.HasFile)
                SaveUploadedfile(FileUpload3, "003.");
            if (FileUpload4.HasFile)
                SaveUploadedfile(FileUpload4, "004.");
            if (FileUpload5.HasFile)
                SaveUploadedfile(FileUpload5, "005.");

            Response.Redirect("/mycars.aspx");
        }

        private  void SaveUploadedfile(FileUpload currentfileupload, string pos )
        {
            string filename = Path.GetFileName(currentfileupload.FileName);
            string fileext = filename.Substring(filename.LastIndexOf(".") + 1);
            currentfileupload.SaveAs(Server.MapPath("~/UploadImages/") + sID + "_" + pos + fileext);
        }

        protected void SqlDataSourceNewCar_Inserted(object sender, SqlDataSourceStatusEventArgs e)
        {
              sID = e.Command.Parameters["@NewCarID"].Value.ToString();
        }





        public string sID { get; set; }
    }
}